import axios from "axios";
import { Message } from "element-ui";
import router from "@/router"
const instance =axios.create({
    baseURL:"http://xue.cnkdl.cn:23683",
    timeout:10000
})

instance.interceptors.request.use(config=>{
    const token = localStorage.getItem("edb-authorization-token")
    if (token && !config.url.endsWith("/login") && !config.url.endsWith("/captchaImage")) {
        config.headers["Authorization"] = "Bearer " + token
    }
    return config
},err=>{
    return Promise.reject(err)
})
instance.interceptors.response.use(res=>{
    if(res.data.code!=200){
        Message.error(res.data.msg || "网络错误")
          if(res.data.code==401){
              localStorage.removeItem("edb-authorization-token")
              router.push("/login")
          }
        return false
    }
    return res
},err=>{
    return Promise.reject(err)
})

export default instance